package com.yuerlive.paperplane;

<<<<<<< HEAD
=======
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
>>>>>>> 694941a2a2816b23c3dc80f3564cc086513ec2ea
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

import com.yuerlive.paperplane.module.main.FavoritesFragment;
import com.yuerlive.paperplane.module.main.InfoFragment;
import com.yuerlive.paperplane.module.main.TimeLineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

import butterknife.BindView;

public class MainActivity extends AppCompatActivity {
    private static final String KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID = "KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID";
<<<<<<< HEAD
    @BindView(R.id.flContainer)
    FrameLayout flContainer;
    @BindView(R.id.bnv)
    BottomNavigationView bnv;
    private TimeLineFragment timeLineFragment;
    private InfoFragment infoFragment;
    private FavoritesFragment favoritesFragment;

=======
    public static final String ACTION_FAVORITES = "com.yuerlive.paperplane.favorite";

    @BindView(R.id.bottom_nav)
    BottomNavigationView bottomNavigationView;
>>>>>>> 694941a2a2816b23c3dc80f3564cc086513ec2ea

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragments(savedInstanceState);
        if(savedInstanceState!=null){
            int id=savedInstanceState.getInt(KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID,R.id.nav_timeline);
            switch (id){
                case R.id.nav_timeline:
                    showFragment(timeLineFragment);
                    break;
                case R.id.nav_info:
                    showFragment(infoFragment);
                    break;
                case R.id.nav_favorites:
                    showFragment(favoritesFragment);
                    break;
            }
        }
    }

    private void showFragment(Fragment fragment) {
        FragmentManager fm=getSupportFragmentManager();
        if(fragment instanceof TimeLineFragment){
            fm.beginTransaction().show(timeLineFragment).hide(infoFragment).
                    hide(favoritesFragment).commit();
        }else if(fragment instanceof  InfoFragment){
            fm.beginTransaction().show(infoFragment).hide(timeLineFragment).
                    hide(favoritesFragment).commit();
        }else if(fragment instanceof  FavoritesFragment){
            fm.beginTransaction().show(favoritesFragment).hide(timeLineFragment)
                    .hide(infoFragment).commit();
        }
    }

    private void initFragments(Bundle savedInstanceState) {
        FragmentManager fm=getSupportFragmentManager();
        if(savedInstanceState==null){
            timeLineFragment = TimeLineFragment.newInstance();
            infoFragment = InfoFragment.newInstance();
            favoritesFragment = FavoritesFragment.newInstance();
        }else{
            timeLineFragment=(TimeLineFragment) fm.getFragment(savedInstanceState,TimeLineFragment.class.getSimpleName());
            infoFragment=(InfoFragment)fm.getFragment(savedInstanceState,InfoFragment.class.getSimpleName());
            favoritesFragment=(FavoritesFragment)fm.getFragment(savedInstanceState,FavoritesFragment.class.getSimpleName());
        }
        if(!timeLineFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,timeLineFragment,TimeLineFragment.class.getSimpleName()).commit();
        }
        if(!infoFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,infoFragment,InfoFragment.class.getSimpleName()).commit();
        }
        if(!favoritesFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,favoritesFragment,FavoritesFragment.class.getSimpleName()).commit();
        }

    }

}
